import { getCategoryList ,  getTagList , getFileList } from '@/services';
import { ICategoryList , ITagList } from '@/types';
import { Effect, Reducer } from 'umi';
export interface OwnspaceModelState {
  categoryList: ICategoryList[];
  tagList: ITagList[];
  fileList:number
}
export interface OwnspacelType {
  namespace: 'ownspace';
  state: OwnspaceModelState;
  effects: {
    getCategoryList: Effect;
    getTagList: Effect;
    getFileList: Effect;
  };
  reducers: {
    save: Reducer<OwnspaceModelState>;
  };
}
const OwnspaceModel: OwnspacelType = {
  namespace: 'ownspace',
  state: {
    categoryList:[],
    tagList:[], 
    fileList:0,
  },
  effects: {
    *getCategoryList({ payload }, { call, put, select }) {
      let result = yield call(getCategoryList, payload);      
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            categoryList: result.data,
          },
        });
      }
    },
    *getTagList({ payload }, { call, put, select }) {
      let result = yield call(getTagList, payload);      
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            tagList: result.data,
          },
        });
      }
    },
    *getFileList({ payload }, { call, put, select }) {
      let result = yield call(getFileList, payload);      
      if (result.data) {
        yield put({
          type: 'save',
          payload: {
            fileList: result.data[1],
          },
        });
      }
    },

  },
  reducers: {
    save(state, action) {
      return {
        ...state,
        ...action.payload,
      };
    },
  },
};

export default OwnspaceModel;
